﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using WaterCloud.Code;
using WaterCloud.Domain.SalaryManagement;
using WaterCloud.Service;
using Microsoft.AspNetCore.Authorization;
using WaterCloud.Service.SalaryManagement;
using WaterCloud.Service.SocialSecurityManagement;
using WaterCloud.Service.WelfaresubsidyManagement;

namespace WaterCloud.Web.Areas.SalaryManagement.Controllers
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-04-23 11:23
    /// 描 述：薪酬方案控制器类
    /// </summary>
    [Area("SalaryManagement")]
    public class PersonsalaryController :  BaseController
    {
        public PersonsalaryService _service {get;set;}
        public WelfaresubsidyService _itemsservice { get; set; }
        #region 获取数据
        [HandlerAjaxOnly]
        [IgnoreAntiforgeryToken]
        public async Task<ActionResult> GetGridJson(SoulPage<PersonsalaryEntity> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "F_Id";
                pagination.order = "desc";
            }
            var data = await _service.GetLookList(pagination,keyword);
           

            if (data.Count > 0)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_WelfareSubsidyNum))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_WelfareSubsidyNum.Split(','))
                        {
                            var temp = await _itemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_WelfareSubsidy = string.Join("  ", str.ToArray());

                    }

                }

            } 
            return Content(pagination.setData(data).ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public async Task<ActionResult> GetListJson(string keyword)
        {
            var data = await _service.GetList(keyword);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public async Task<ActionResult> GetFormJson(string keyValue)
        {
            var data = await _service.GetLookForm(keyValue);
            List<string> str = new List<string>();
            if (!string.IsNullOrEmpty(data.F_WelfareSubsidyNum))
            {
                foreach (var item in data.F_WelfareSubsidyNum.Split(','))
                {
                    var temp = await _itemsservice.GetForm1(item);
                    if (temp != null)
                    {
                        str.Add(temp.F_FullName);

                    }
                }
            
            data.F_WelfareSubsidy = string.Join("  ", str.ToArray());
             }

            return Content(data.ToJson());
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [HandlerAjaxOnly]
        public async Task<ActionResult> SubmitForm(PersonsalaryEntity entity, string keyValue)
        {
            try
            {
                await _service.SubmitForm(entity, keyValue);
                return await Success("操作成功。", "", keyValue);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", keyValue);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        public async Task<ActionResult> DeleteForm(string keyValue)
        {
            try
            {
                await _service.DeleteForm(keyValue);
                return await Success("操作成功。", "", keyValue, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", keyValue, DbLogType.Delete);
            }
        }
        #endregion
    }
}
